package com.conversationboard.api;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.SecurityContext;

import com.conversationboard.api.model.UserXML;
import com.conversationboard.api.security.Security;
import com.conversationboard.model.User;

public class UserInfoProcessor implements Processor {

	/* Input Form: <UserInfo></UserInfo> */

	@Override
	public String processXml(String xml, SecurityContext securityContext, HttpServletRequest request) throws ApiException {

		if (!Security.isLoggedIn(securityContext)) {
			return "<UserInfo><Error>Not logged in</Error></UserInfo>";
		}
		
		User user = null;

		try {
			user = User.get(request.getUserPrincipal());
		} catch (SQLException e) {
			throw new ApiException(e);
		}

		if (user.isLoggedIn()) {
			return UserXML.getUserXML(user, null);
		} else {
			return "<UserInfo><Error>Couldn't get a handle on the user; maybe not logged in</Error></UserInfo>";
		}

	}

}
